package com.lhn.bbs.controller;
import com.lhn.bbs.annotation.JwtAnnotation;
import com.lhn.bbs.dto.ActionResult;
import com.lhn.bbs.dto.LoginVo;
import com.lhn.bbs.dto.UserVo;
import com.lhn.bbs.entity.User;
import com.lhn.bbs.service.UserService;
import com.lhn.bbs.utils.MailUtils;
import com.lhn.bbs.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@RestController
@Slf4j
@RequestMapping("/log")
public class LoginAndRegistController {
    @Autowired(required = false)
    private UserService userService;
    //登录
    @JwtAnnotation(required = true)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ActionResult login(@RequestBody LoginVo userVo, HttpSession session){
        log.info("======================login:uservo"+userVo);
        String code = userVo.getImgCode();
        log.info("=====================login:code"+code);
        String verifyCode = (String) session.getAttribute("verifyCode");
//        判断验证码是否一致
        if (code.equalsIgnoreCase(verifyCode)){
            session.removeAttribute("verifyCode");
//            根据用户的账号查询用户是否存在
            User user = userService.findBySno(userVo.getSno());
            log.info("============user :"+user);
//            判断用户密码是否一致
            if (user!=null && user.getPassword().equals(userVo.getPassword())){
                HashMap map = new HashMap();
                map.put("user",user);
                return new ActionResult(200,"登录成功",map,null);
            }else{
                return new ActionResult(500,"用户名或密码错误",null,null);
            }
        }else{
            return new ActionResult(404,"验证码错误",null,null);
        }
    }
    //注册用户
//    @JwtAnnotation(required = true)
    @RequestMapping(value = "register",method = RequestMethod.POST)
    public ActionResult register(@RequestBody UserVo user, HttpSession session){
        log.info("==========user:"+user);
        session.removeAttribute("checkCode");
        try {
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            String format = formatter.format(date);
            user.setRegisterdate(format);
            Boolean flag = userService.addUser(user);
            log.info("=========flag---"+flag);
            if (flag) {
                return new ActionResult(200, "注册成功", null, null);
            } else {
                return new ActionResult(500, "注册失败", null, null);
            }
        }catch (Exception e){
            return new ActionResult(500, "注册失败", null, null);
        }
    }
    //注册验证学号是否已经被注册
//    @JwtAnnotation(required = true)
    @RequestMapping(value = "checksno",method = RequestMethod.POST)
    public ActionResult checksno(String sno){
        log.info("==========sno ::"+sno);

        User user = userService.findBySno(sno);
        log.info("==========user:sno=="+user);
        if (user!=null){
            return new ActionResult(200,"学号已经被注册",null,null);
        }else{
            return new ActionResult(404,"学号可用",null,null);
        }

    }
    //发送邮箱
    @JwtAnnotation(required = true)
    @PostMapping("/sendEmail")
    public ActionResult sendEmail(String email, HttpSession session){
        log.info("=========email"+email);
        String checkCode = UUIDUtils.getCheckCode();
        if (email!=null){
            session.setAttribute("checkCode",checkCode);
            boolean b = MailUtils.sendMail(email, "邮箱验证码:" + checkCode, "注册邮箱验证码");
            if (b==true){
                return new ActionResult(200,"注意查收邮箱验证码",checkCode,null);
            }else{
                return new ActionResult(404,"邮箱不正确",null,null);
            }
        }else{
            return null;
        }

    }
}
